package cn.impl;

import cn.annotation.Log;
import cn.basic.HibBaseDAOImpl;
import cn.basic.iHibBaseDAO;
import cn.common.properties.LogOperType;
import cn.dao.SystemLogDAO;
import cn.model.TSystemLog;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component("systemlogdao")
public class SystemLogDAOImpl implements SystemLogDAO {

    //连接数据库操作层接口,实现业务与数据的交换
    private iHibBaseDAO bdao= null;

    public SystemLogDAOImpl(){
        bdao = new HibBaseDAOImpl();
    }


    @Log(isSaveLog=true,operationType= LogOperType.ADD,operationName="添加一条日志信息")
    @Override
    public Integer addLog(TSystemLog record) {
        return (Integer)bdao.insert(record);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询日志信息列表")
    @Override
    public List<TSystemLog> getSystemLogList(String wherecondition, int currentPage, int pageSize) {
        String hql = "from TSystemLog";
        if (wherecondition == null && wherecondition.equals("")){
            hql += "order by createdate desc";
        }
        if (wherecondition != null && !wherecondition.equals("")){
            hql += wherecondition;
        }
        List<TSystemLog> list = bdao.selectByPage(hql, currentPage, pageSize);
        return list;
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询日志信息记录条数")
    @Override
    public int getSystemLogAmount(String wherecondition) {
        String hql = "select count(*) from TSystemLog ";
        if(wherecondition!=null && !wherecondition.equals("")){
            hql += wherecondition;
        }
        return bdao.selectValue(hql);
    }

    @Override
    public List<String> getOperTypes() {
        List<String> list = new ArrayList<String>();
        for(int i = 0; i< LogOperType.OPERTYPES.length; i++){
            list.add(LogOperType.OPERTYPES[i]);
        }

        return list;
    }

    @Log(isSaveLog=true,operationType= LogOperType.DELETE,operationName="删除一条日志记录")
    @Override
    public boolean deleteLogById(String id) {
        return bdao.delete(TSystemLog.class, id);
    }



    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="通过编号查询日志信息")
    @Override
    public TSystemLog getLogById(String id) {
        return (TSystemLog)bdao.findById(TSystemLog.class, id);
    }

    @Log(isSaveLog=true,operationType= LogOperType.DELETE,operationName="清除所有日志")
    @Override
    public boolean deleteAllLog(){
        String hql = "Delete from T_SystemLog ";
        if (bdao.delete(hql)) {
            return true;
        }else {
            return false;
        }
    }
}
